String g;
int temp; //温度值
int light; //光照值
void setup() {
  // put your setup code here, to run once:
  
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6,OUTPUT);
  Serial.begin(9600);
}

int commaPosition;
    String c1;
void loop() {
    loadTemp();
    Serial.print(',');
    loadLight();
    if(Serial.available() >0){
      g = Serial.readString();
    }
      commaPosition = g.indexOf(',');
      if(commaPosition != -1)
      {
        c1 = g.substring(0, commaPosition);
        g = g.substring(commaPosition + 1, g.length());
        if(c1 == "a"){
          commaPosition = g.indexOf(",");
          c1 = g.substring(0, commaPosition);
          g = g.substring(commaPosition + 1, g.length());
          int i = c1.toInt();
          analogWrite(9, i);
        }else if(c1 == "b"){
          commaPosition = g.indexOf(",");
          c1 = g.substring(0, commaPosition);
          g = g.substring(commaPosition + 1, g.length());
          int i = c1.toInt();
          analogWrite(10, i);
        }else if(c1 == "c"){
          commaPosition = g.indexOf(",");
          c1 = g.substring(0, commaPosition);
          g = g.substring(commaPosition + 1, g.length());
          int i = c1.toInt();
          analogWrite(5, i);
        }else if(c1 == "d"){
          commaPosition = g.indexOf(",");
          c1 = g.substring(0, commaPosition);
          g = g.substring(commaPosition + 1, g.length());
          int i = c1.toInt();
          analogWrite(6, i);
        }
      }
}
void loadTemp(){
  temp = analogRead(A0);
  temp = (temp * 5) / 10; 
  Serial.print(temp);
}
void loadLight(){
  light = analogRead(A1);
  Serial.println(light);
}

